from cffi import FFI
import os
import sys


ffi = FFI()

ffi.cdef('''
    int get_rtsp_url(char* rtsp_url1, char* rtsp_url2, const char *ip, const char *username, const char *password);
''')

LIB_NAME = 'libipconvif.so'


class Ipconvif():
    def __init__(self, ip, username, passwd):
        self.rtsp_url1 = ffi.new("char[]", 1024) # first url
        self.rtsp_url2 = ffi.new("char[]", 1024) # second url
        self.ip = ffi.new("char[]", ip.encode())
        self.username = ffi.new("char[]", username.encode())
        self.passwd = ffi.new("char[]", passwd.encode())
        
        self.lib = None
        self.load_library()
    
    def load_library(self):
        lib_path = LIB_NAME
        if os.path.exists(lib_path):
            self.lib = ffi.dlopen(lib_path)
            return True
        return False
        
    def get_rtsp_url(self):
        self.lib.get_rtsp_url(self.rtsp_url1, self.rtsp_url2, self.ip, self.username, self.passwd)
        return ffi.string(self.rtsp_url1), ffi.string(self.rtsp_url2)
        
                
        
if __name__ == "__main__":
    ipc = Ipconvif('192.21.1.201', 'admin', '123cpucpu')
    urls = ipc.get_rtsp_url();
    print(urls[0])
    print(urls[1])